@(activities: List[gitbucket.core.model.Activity])(implicit context: gitbucket.core.controller.Context)
@import gitbucket.core.view.helpers

@if(activities.isEmpty){
  No activity
} else {
  @activities.map { activity =>
    <div class="block">
      @(activity.activityType match {
        case "open_issue"        => detailActivity(activity, "issue-opened")
        case "comment_issue"     => detailActivity(activity, "comment-discussion")
        case "comment_commit"    => detailActivity(activity, "comment-discussion")
        case "close_issue"       => detailActivity(activity, "issue-closed")
        case "reopen_issue"      => detailActivity(activity, "issue-reopened")
        case "open_pullreq"      => detailActivity(activity, "git-pull-request")
        case "merge_pullreq"     => detailActivity(activity, "git-merge")
        case "release"           => detailActivity(activity, "package")
        case "create_repository" => simpleActivity(activity, "repo")
        case "create_branch"     => simpleActivity(activity, "git-branch")
        case "delete_branch"     => simpleActivity(activity, "circle-slash")
        case "create_tag"        => simpleActivity(activity, "tag")
        case "delete_tag"        => simpleActivity(activity, "circle-slash")
        case "fork"              => simpleActivity(activity, "repo-forked")
        case "push"  => customActivity(activity, "git-commit"){
          <div class="small activity-message">
            {activity.additionalInfo.get.split("\n").reverse.take(4).zipWithIndex.map{ case (commit, i) =>
              if(i == 3){
                <div>...</div>
              } else {
                if(commit.nonEmpty){
                  <div>
                     <a href={s"${context.path}/${activity.userName}/${activity.repositoryName}/commit/${commit. substring(0, 40)}"} class="monospace">{commit.substring(0, 7)}</a>
                     <span>{commit.substring(41)}</span>
                  </div>
                }
              }
            }}
          </div>
        }
        case "create_wiki" => customActivity(activity, "book"){
          <div class="small activity-message">
            Created <a href={s"${context.path}/${activity.userName}/${activity.repositoryName}/wiki/${activity.additionalInfo.get}"}>{activity.additionalInfo.get}</a>.
          </div>
        }
        case "edit_wiki" => customActivity(activity, "book"){
          activity.additionalInfo.get.split(":") match {
            case Array(pageName, commitId) =>
              <div class="small activity-message">
                Edited <a href={s"${context.path}/${activity.userName}/${activity.repositoryName}/wiki/${pageName}"}>{pageName}</a>.
                <a href={s"${context.path}/${activity.userName}/${activity.repositoryName}/wiki/${pageName}/_compare/${commitId.substring(0, 7)}^...${commitId.substring(0, 7)}"}>View the diff »</a>
              </div>
            case Array(pageName) =>
              <div class="small activity-message">
                Edited <a href={s"${context.path}/${activity.userName}/${activity.repositoryName}/wiki/${pageName}"}>{pageName}</a>.
              </div>
          }
        }
      })
    </div>
  }
}

@detailActivity(activity: gitbucket.core.model.Activity, image: String) = {
  @*
  <div class="activity-icon-large"><i class="mega-octicon octicon-@image"></i></div>
  *@
  <div>
    <div class="muted small">@gitbucket.core.helper.html.datetimeago(activity.activityDate)</div>
    <div class="strong">
      @helpers.avatarLink(activity.activityUserName, 16)
      @helpers.activityMessage(activity.message)
    </div>
    @activity.additionalInfo.map { additionalInfo =>
      <div class=" activity-message">@additionalInfo</div>
    }
  </div>
}

@customActivity(activity: gitbucket.core.model.Activity, image: String)(additionalInfo: Any) = {
  @*
  <div class="activity-icon-large"><i class="mega-octicon octicon-@image"></i></div>
  *@
  <div>
    <div class="muted small">@gitbucket.core.helper.html.datetimeago(activity.activityDate)</div>
    <div class="strong">
      @helpers.avatarLink(activity.activityUserName, 16)
      @helpers.activityMessage(activity.message)
    </div>
    @additionalInfo
  </div>
}

@simpleActivity(activity: gitbucket.core.model.Activity, image: String) = {
  @*
  <div class="activity-icon-small"><i class="octicon octicon-@image"></i></div>
  *@
  <div>
    <span class="muted small">@gitbucket.core.helper.html.datetimeago(activity.activityDate)</span>
    <div>
      @helpers.avatarLink(activity.activityUserName, 16)
      @helpers.activityMessage(activity.message)
    </div>
  </div>
}

